package com.liuzu.esys.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.GoodBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.listener.WebGoodListener;

import com.liuzu.esys.pojo.Good;
import com.liuzu.esys.service.GoodService;
import com.liuzu.esys.util.OssUtil;
import com.liuzu.esys.vo.TypeBarVo;
import com.liuzu.esys.vo.TypePieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    @RequestMapping("/goodList")
    //public LayuiPageVo<Student> studentPageData(Integer page,Integer limit){
    // 注意 这里的  @RequestParam 不能省略   省略的话就不能获取参数了
    public LayuiPageVo<Good> studentPageData(@RequestParam Map<String,Object> params){
        System.out.println(params);
        return goodService.goodPageData(params);
    }

    @RequestMapping("/userPushList")
    //public LayuiPageVo<Student> studentPageData(Integer page,Integer limit){
    // 注意 这里的  @RequestParam 不能省略   省略的话就不能获取参数了
    public LayuiPageVo<Good> userPushPageData(@RequestParam Map<String,Object> params){
        System.out.println(params);
        return goodService.userPushPageData(params);
    }


    @RequestMapping("/deleteGood")
    public R deleteGood(Integer id){
        int result = goodService.deleteGood(id);
        if (result==1){
            return R.ok();
        }
        else {
            return R.error();
        }
    }

    @RequestMapping("/updateGood")
    public R updateGood(Good good){
        System.out.println(good);

        int result = goodService.updateGood(good);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @RequestMapping("/addGood")
    public R addGood(Good good){
        System.out.println(good);

        int result = goodService.addGood(good);
        if(result==1){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        if(file!=null){
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtil.uploadFile(inputStream, originalFilename);

            return R.ok().data("url",url);
        }
        return R.ok();
    }


    @RequestMapping("/deleteSelectGood")
    public R deleteSelectGood(String ids){
        int result = goodService.deleteSelectGood(ids);
        if (result>0){
            return R.ok();
        }
        else {
            return R.error();
        }
    }


    //下载excel 模板
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/template/商品信息模板.xlsx");

        //根据文件路径 创建File 对象
        File file = new File(realPath);

        // 把File  对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("商品信息模板", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();
    }


    @Autowired
    private WebGoodListener webGoodListener;
    // 读取excel 文件数据 存到数据库中
    @RequestMapping("import")
    public R importExcel(MultipartFile file) throws IOException{
        if (file!=null){
            /**
             * 参数1  文件流
             * 参数2  读取到的数据封装到哪个数据类型中
             * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
             * */
            ExcelReaderBuilder read  = EasyExcel.read(file.getInputStream(), GoodBo.class,webGoodListener);
            //得到sheet对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            //读取
            sheet.doRead();
        }
        return R.ok();
    }

    @RequestMapping("/export")
    public void export(Good good, HttpServletResponse response,@RequestParam Map<String,Object> params) throws IOException{
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("商品信息导入","utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        // 得到指向游览器的输出流 下载文件
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, GoodBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        List<GoodBo> goods = goodService.goodExcelData(good,params);

        sheet.doWrite(goods);

    }

    @RequestMapping("/echartBar")
    public R echartBar(){
        List<TypeBarVo> list = goodService.typeBarData();
        return R.ok().data("typeBar",list);
    }

    @RequestMapping("/echartsPie")
    public R echartsPie() {

        List<TypePieVo> list = goodService.typePieData();

        return R.ok().data("typePie", list);
    }
}
